<template>
  <div class="login-container">
    <div class="login-logo">
      <img src="../../logo.png" alt="">
      <span class="login-title">芸助手</span>
      <div class="login-line"></div>
      <span class="login-subTitle">商户平台</span>
    </div>
    <el-row class="login-item" style="display: flex;justify-content: center">
      <el-col :span="10" class="login-left" style="box-shadow: 0 4px 10px 0 rgb(0 0 0 / 3%);">
        <div>
          <img class="login-banner" src="./images/login-banner.png" alt="">
        </div>
      </el-col>
      <el-col :span="10" class="login-right" style="box-shadow: 0 4px 10px 0 rgb(0 0 0 / 3%);">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
                 label-position="left">
          <div class="title-container">
            <div class="login-switch-left" :class="switchType?'login-switch-choose':''" @click="switchClick(true)">
              <span class="login-switch-left-title">商户登录</span>
            </div>
            <div class="login-switch-right" :class="!switchType?'login-switch-choose':''" @click="switchClick(false)">
              <span class="login-switch-left-title">代理商登录</span>
            </div>
          </div>
          <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon style="font-size: 14px" icon-class="index_member"/>
        </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              :placeholder="$t('手机号/邮箱')"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>
          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon style="font-size: 12px;margin-bottom: 2px" icon-class="password"/>
          </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                :placeholder="$t('login.password')"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
            <svg-icon style="font-size: 12px;" :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
          </span>
            </el-form-item>
          </el-tooltip>
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-input type="text" placeholder="请输入验证码" class="verifyCode" v-model="loginForm.verifyCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11" :offset="1">
              <div>
                <div class="codeImg" @click="createCaptcha">
                  <img :src="codeImg" width="100%" height="100%" alt="">
                </div>
              </div>
            </el-col>
          </el-row>
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:20px;"
                     @click.native.prevent="handleLogin">
            {{ $t('login.logIn') }}
          </el-button>
          <el-row justify="center">
            <el-col :span="8" align="center">
              <a class="label-button" @click="forget">
                <svg-icon style="font-size: 10px;margin-bottom: 4px" icon-class="password"/>
                <span>&nbsp忘记密码</span></a>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
    <div class="login-bottom ">
      <p>用技术连接和重构实体产业生态圈</p>
      <p>Copyright ©20 20 睿商(厦门)科技股份有限公司 All Rights Reserved. <a href="https://beian.miit.gov.cn/">闽ICP备13006726号</a> </p>
    </div>

    <el-dialog title="温馨提示" :visible.sync="membershipDialogFormVisible" width="300px">
      <div v-if="expired==0">
        <span>您的店铺已到期,请联系客户经理缴费.</span>
      </div>
      <div v-else>
        <span>您的店铺将于{{expired}}天后到期,请联系客户经理缴费.</span>
      </div>
      <div slot="footer" class="dialog-footer">
<!--        <el-button @click="membershipDialogFormVisible = false">取 消</el-button>-->
        <el-button type="primary" @click="saveClick">我知道了</el-button>
      </div>
    </el-dialog>

<!--    <el-dialog-->
<!--      title="微信扫码支付"-->
<!--      :visible.sync="dialogData.dialogVisible"-->
<!--      width="20%"-->
<!--      v-dialogDrag-->
<!--      center-->
<!--      @close="close">-->
<!--      <div id="qrcode" ref="qrcode" style="display:flex;justify-content:center">-->
<!--      </div>-->
<!--&lt;!&ndash;      <p style="text-align:center;margin:5px"><el-button type="primary" size="mini" @click="refresh">刷新二维码</el-button></p>&ndash;&gt;-->
<!--&lt;!&ndash;      <p style="text-align:center"><b>订单名称:321</b></p>&ndash;&gt;-->
<!--&lt;!&ndash;      <p style="text-align:center"><b>订单价格:321元</b></p>&ndash;&gt;-->
<!--      <div slot='footer' class="dialog-footer">-->
<!--        <p style="text-align:center"><b>尊敬的客户您的会员有效期已过请扫码续费</b></p>-->
<!--        &lt;!&ndash;        <el-button type="primary" @click="confirm">已支付</el-button>&ndash;&gt;-->
<!--&lt;!&ndash;        <el-button type="warning" @click='cancel'>取消</el-button>&ndash;&gt;-->
<!--      </div>-->
<!--    </el-dialog>-->



  </div>
</template>
<script>
import utils from '../../utils/utils.js'
import ElRow from "element-ui/packages/row/src/row";
import ElCol from "element-ui/packages/col/src/col";
import { isExpired , payYearFee } from "@/api/payFee";

export default {
  components: {
    ElCol,
    ElRow
  },
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      expired:'',
      membershipDialogFormVisible:false,
      dialogData:{
        dialogVisible:true
      },
      loginForm: {
        username: '',
        password: '',
        verifyCode: '',
        verifyCodeKey: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur'}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}],
        verifyCode: [{required: true, trigger: 'blur'}]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {},
      switchType:true
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    this.loginForm.verifyCodeKey = utils.getUUID()
    this.createCaptcha()
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    saveClick(){
      if(this.expired > 0){
        this.$router.push({path: '/', query: this.otherQuery})
        this.membershipDialogFormVisible = false
      }
      if(this.expired == 0 ){
        this.$store.commit("user/SET_TOKEN", "")
        this.$store.dispatch('user/logout')
        this.$router.push(`/login?redirect=${this.$route.fullPath}`)
        this.membershipDialogFormVisible = false
      }
    },
    forget() {
      this.$router.push('/password')
    },
    register() {
      this.$router.push('/register')
    },
    // 图片验证码
    createCaptcha() {
      var _this = this
      _this.codeImg = process.env.VUE_APP_BASE_API + '/user/getVerifyCode?verifyCodeKey=' + _this.loginForm.verifyCodeKey + '&date=' + new Date().getTime()
    },
    checkCapslock(e) {
      const {key} = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    validationExpired(){
      this.$store.commit("user/SET_MCHID", "")
      isExpired().then(res=>{
        this.expired = res.data
        if( res.data ==1 || res.data ==2 || res.data ==3 || res.data ==4 || res.data ==5 || res.data ==6 || res.data ==7){
          this.membershipDialogFormVisible = true
        }
        if(res.data == 0){
          let params = {
            paymentPluginId:'weixinPayPlugin'
          }
          payYearFee(params).then(res=>{
            console.log(res,"fdsfdse2321321")
          })
          this.membershipDialogFormVisible = true
        }
        if(res.data == -1 || res.data > 7){
          this.$router.push({path: '/', query: this.otherQuery})
        }
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              console.log('login success!!')
              this.createCaptcha()
              this.validationExpired()
              // this.$router.push({path: '/', query: this.otherQuery})
              this.loading = false
            })
            .catch(() => {
              console.log('login error!!')
              this.createCaptcha()
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          this.createCaptcha()
          this.loading = false
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    switchClick(data){
//      var _this = this
//      if(data == false){
//        window.location.href = "http://agent.rzico.com";
//      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #000; //#283443;

$light_gray: #2d3a4b;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: #000)) {
  .login-container .el-input input {
    color: #000;
  }
}


/* reset element-ui style */
.login-container {
  position: relative;
  // background: #dfe6ec;
  .el-input {
    display: inline-block;
    height: 42px;
    width: 85%;

    input {
      background: #fff;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 42px;
      //caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: #000 !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    //background:$bg;
    border-radius: 5px;
    color: #454545;
    margin-bottom: 20px;
  }
}
</style>

<style lang="scss" scoped>
//$bg:#2d3a4b;
//$bg:#fff;
$bg: #fafafa;
$dark_gray: #889aa4;
$light_gray: #2d3a4b;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .login-logo {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 405px;
    height: 100px;
    padding: 50px 40px 10px 40px;
    justify-content: flex-start;

    img {
      width: 52px;
      height: 52px;
    }

    .login-title {
      margin-left: 20px;
      font-size: 28px;
      color: #333333;
    }

    .login-line{
      width: 1px;
      height: 20px;
      background-color: rgb(230,230,230);
      margin-left: 20px;
    }

    .login-subTitle{
      margin-left: 20px;
      font-size: 22px;
      color: #333333;
      font-weight: bold;
    }
  }

  .login-left {
    width: 400px;
    height: 400px;
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
    overflow: hidden;
    border-top: 1px solid #e7e9eb;
    border-bottom: 1px solid #e7e9eb;
    border-left: 1px solid #e7e9eb;
    .login-banner {
      width: 100%;
    }
  }

  .login-right {
    width: 400px;
    height: 400px;
    padding: 0 50px;
    position: relative;
    background: #fff;
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;
    border-top: 1px solid #e7e9eb;
    border-bottom: 1px solid #e7e9eb;
    border-right: 1px solid #e7e9eb;
  }

  .login-form {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 320px;
    /*max-width: 100%;*/
    //padding: 120px 0px 0;
    margin: 0 0;
    overflow: hidden;
  }

  .label-button {
    border-bottom: 0px solid #dfe6ec;
    padding-bottom: 10px;
    margin: 10px auto;
    font-weight: bold;
    color: $light_gray;
  }

  .tips {
    margin-bottom: 20px;
    font-size: 14px;
    color: #fff;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .codeImg {
    height: 42px;
  }

  .svg-container {
    padding: 0px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    box-sizing: border-box;
    line-height: 42px;
  }

  .title-container {
    position: relative;
    display: flex;
    flex-direction: row;
    margin-bottom: 20px;
    justify-content: center;
    .title {
      font-size: 20px;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      /*font-weight: bold;*/
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }

    .login-switch-left{
      width: 106px;
      height: 32px;
      border: 1px solid #eee;
      font-size: 13px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 2px;
      border-right-color: #b3d8ff;
    }
    .login-switch-left:hover{
      color: #409eff;
    }

    .login-switch-right{
      width: 106px;
      height: 32px;
      border: 1px solid #eee;
      font-size: 13px;
      text-align: center;
      border-left: none;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 2px;
    }
    .login-switch-right:hover{
      color: #409eff;
    }

    .login-switch-choose{
      border-color: #b3d8ff;
      color: #409eff;
    }
  }

  .show-pwd {
    position: absolute;
    right: 20px;
    top: 0px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    line-height: 42px
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 0px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
    .thirdparty-button {
      display: none;
    }
  }


  .pan-back-btn {
    background: #008489;
    color: #fff;
    border: none !important;
  }

  .pan-gif {
    margin: 0 auto;
    display: block;
  }

  .pan-img {
    display: block;
    margin: 0 auto;
    width: 100%;
  }

  .text-jumbo {
    font-size: 60px;
    font-weight: 700;
    color: #484848;
  }

  .list-unstyled {
    font-size: 14px;

    li {
      padding-bottom: 5px;
    }

    a {
      color: #008489;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  .login-bottom {
    line-height: 30px;
    text-align: center;
    margin-bottom: 30px;

    p:nth-child(1) {
      font-size: 17px;
      color: $light_gray;
    }

    p:nth-child(2) {
      font-size: 10px;
      color: $light_gray;
    }
  }
}

</style>
